package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateBalanceAccountByPageDTO;
import com.itlong.cloud.POJO.DTO.operate.WithdrawCashAndRechargeDTO;
import com.itlong.cloud.POJO.PO.AmountChangeInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateBalanceAmountGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.chinapay.service.IChinaPayTransferAccountService;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.TradeTypeEnum;
import com.itlong.cloud.operate.dao.IOperateBalanceAccountDao;
import com.itlong.cloud.operate.service.IOperateBalanceAccountService;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 * 运营-财务平账服务暴露接口实现类.
 * </desc>
 *
 * @createDate 2018/9/20
 */
@Service
public class OperateBalanceAccountServiceImpl implements IOperateBalanceAccountService {

    @Autowired
    IOperateBalanceAccountDao iOperateBalanceAccountDao;

    @Autowired
    IChinaPayTransferAccountService iChinaPayTransferAccountService;

    @Autowired
    private MessageSender messageSender;
    /**
     * <desc>
     * 分页查询平账列表
     * <desc/>
     *
     * @param balanceAccountByPageDTO
     * @return
     * @author Juguang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateBalanceAmountGetPageVO> getInfoByPage(OperateBalanceAccountByPageDTO balanceAccountByPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(balanceAccountByPageDTO);
        Page<OperateBalanceAmountGetPageVO> page = new Page<>();
        page.getPage().setPageSize(balanceAccountByPageDTO.getPageSize());
        page.getPage().setCurrentPage(balanceAccountByPageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<OperateBalanceAmountGetPageVO> datas = iOperateBalanceAccountDao.getBalanceAccountInfoByPage(params);
        page.setRows(datas,iOperateBalanceAccountDao.getBalanceAccountInfoCount(params));
        return page;
    }

    /**
     * <desc>
     * 导出EXCEL文件
     * <desc/>
     *
     * @param balanceAccountByPageDTO
     * @return
     * @author Juguang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateBalanceAmountGetPageVO> exportFile(OperateBalanceAccountByPageDTO balanceAccountByPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(balanceAccountByPageDTO);
        return iOperateBalanceAccountDao.getBalanceAccountInfoExport(params);
    }

    /**
     * <desc>
     * 根据ID获取备注信息
     * <desc/>
     *
     * @param changeId 查询ID
     * @return
     * @author Juguang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getRemark(String changeId) throws Exception {
        return iOperateBalanceAccountDao.getRemark(changeId);
    }

    /**
     * <desc>
     * 修改备注信息
     * <desc/>
     *
     * @param changeId 查询ID
     * @return
     * @author Juguang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void updateRemark(String changeId, String myUserName,String remark) throws Exception{
        iOperateBalanceAccountDao.updateRemark(changeId,myUserName,remark);
    }

    /**
     * <desc>
     * 财务平账-保存微信提现和备付金充值记录
     * <desc/>
     *
     * @param withdrawCashAndRechargeDTO 添加信息
     * @return
     * @author Juguang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer save(WithdrawCashAndRechargeDTO withdrawCashAndRechargeDTO) throws Exception {
        BigDecimal decimalAmount = new BigDecimal(withdrawCashAndRechargeDTO.getAmount());
        AmountChangeInfoPO changeInfoPO = new AmountChangeInfoPO(withdrawCashAndRechargeDTO.getChangeId(),
                decimalAmount,withdrawCashAndRechargeDTO.getOutOrderNum(),
                withdrawCashAndRechargeDTO.getMyUserName(),withdrawCashAndRechargeDTO.getRemark());
        changeInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        changeInfoPO.setCreateTime(new Date());
        switch (TradeTypeEnum.getByTradeType(Integer.parseInt(withdrawCashAndRechargeDTO.getTradeType()))){
            case WECHAT_WITHDRAW_FEE:
                BigDecimal handingFee = null;
                //查询第三方管理中是否添加微信提现的手续费
                /*if(StringUtils.isEmpty(iOperateBalanceAccountDao.getWechatFee())){
                    handingFee = new BigDecimal("0.006");
                }else{
                    handingFee = new BigDecimal(iOperateBalanceAccountDao.getWechatFee()).divide(new BigDecimal("100"));
                }*/

                //查询此时微信商户号的余额
                BigDecimal wechatMchiAccount = iOperateBalanceAccountDao.getWechatMchiAccount();
                //扣出手续费的提现金额
                //BigDecimal accountFee = BigDecimal.valueOf(decimalAmount.multiply(handingFee).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                changeInfoPO.setThirdPartyPayment(1);
                changeInfoPO.setTradeBeforeAmount(wechatMchiAccount);
                changeInfoPO.setTradeAmount(decimalAmount);
                //changeInfoPO.setHandlingFee(accountFee);
                changeInfoPO.setTradeAfterAmount(wechatMchiAccount.subtract(decimalAmount));
                changeInfoPO.setTradeType(TradeTypeEnum.WECHAT_WITHDRAW_FEE.getType());
                //资金变动明细记录保存 微信提现
                iOperateBalanceAccountDao.save(SqlUtil.durableData(changeInfoPO, PlatformConstants.TABLE_SAVE));
                break;
            case RECHARGE_FEE:
                String accountBalance = iChinaPayTransferAccountService.getAccountBalance();
                changeInfoPO.setTradeBeforeAmount(BigDecimal.valueOf(Double.valueOf(accountBalance)/100).subtract(decimalAmount));
                changeInfoPO.setTradeAfterAmount(BigDecimal.valueOf(Double.valueOf(accountBalance)/100));
                changeInfoPO.setThirdPartyPayment(2);
                changeInfoPO.setTradeType(TradeTypeEnum.RECHARGE_FEE.getType());
                changeInfoPO.setHandlingFee(new BigDecimal("0"));
                return iOperateBalanceAccountDao.save(SqlUtil.durableData(changeInfoPO, PlatformConstants.TABLE_SAVE));
            default:
                    break;
        }
        return null;
    }

    /**
     * <desc>
     * 查询公司微信商户号的余额
     * <desc/>
     *
     * @return
     * @author Juguang.S
     * @createDate 2018-09-25
     */
    @Override
    public BigDecimal getWechatMchiAccount() {
        return iOperateBalanceAccountDao.getWechatMchiAccount();
    }
}